package com.project.picxg.service.Impl;

import com.project.picxg.dao.StorageDao;
import com.project.picxg.entity.Image;
import com.project.picxg.service.StorageService;
import com.project.picxg.util.FileUtil;
import com.project.picxg.util.SuffixBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Slf4j
@Service
public class StorageServiceImpl implements StorageService {
    private static List<SuffixBean> suffixes = new ArrayList<SuffixBean>();

    static {
        suffixes.add(new SuffixBean("jpg"));
        suffixes.add(new SuffixBean("jpeg"));
        suffixes.add(new SuffixBean("gif"));
        suffixes.add(new SuffixBean("png"));
        suffixes.add(new SuffixBean("bmp"));
        suffixes.add(new SuffixBean("webp"));
        suffixes.add(new SuffixBean("mp4"));

        suffixes.add(new SuffixBean("JPG"));
        suffixes.add(new SuffixBean("JPEG"));
        suffixes.add(new SuffixBean("GIF"));
        suffixes.add(new SuffixBean("PNG"));
        suffixes.add(new SuffixBean("BMP"));
        suffixes.add(new SuffixBean("WEBP"));
        suffixes.add(new SuffixBean("MP4"));
    }

    @Value(value = "${imageFolder}")
    private String path;
    @Resource
    private StorageDao storageDao;

    @Override
    public String upload(File file) throws IOException {
        return upload(new BufferedInputStream(new FileInputStream(file)), file.getName());
    }

    @Override
    public String upload(InputStream is, String filename) throws IOException {
        String suffix = FileUtil.getSuffix(filename);
        String savePath = path;
        String saveDate = getCurrentSaveDate();
        if (!suffixes.contains(new SuffixBean(suffix))) {
            log.error("上传的文件不是图像文件");
            throw new IllegalArgumentException("您上传的文件不是图像文件.");
        }
        String name = FileUtil.getFileName() + (suffix.equals("") ? "" : "." + suffix);
        Path targetPath = new File(savePath + File.separator + saveDate, name).toPath();
        Files.copy(is, targetPath);
        Image image = new Image();
        image.setName(name);
        image.setPath(targetPath.toAbsolutePath().toString());
        image.setUrl(saveDate + "/" + name);
        int i = storageDao.addImage(image);
        if (i == 1) {
            log.info("上传成功");
        }
        return  name;
    }

    @Override
    public Image getUrl(String filename) {
        Image image = storageDao.getImage(filename);
        return image;
    }

    public String getCurrentSaveDate() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);

        // I don't known why calendar start with 0
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        String monthStr = String.valueOf(month);
        if (month < 10) {
            monthStr = "0" + monthStr;
        }
        String dayStr = String.valueOf(day);
        if (day < 10) {
            dayStr = "0" + dayStr;
        }
        String path = year + File.separator + monthStr + File.separator
                + dayStr;

        String savePath = this.path;
        if (savePath.endsWith(File.separator)) {
        } else {
            savePath += File.separator;
        }
        savePath += path;
        File file = new File(savePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return path;
    }
}
